﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.Entity;
using WCFThree.Entity;
using WCFThree.Abstract;

namespace WCFThree.Factory
{
    public class StudentOperate : BaseOperate, IStudent
    {
        /// <summary>
        /// 获取表达式树
        /// </summary>
        /// <returns></returns>
        public DbSet<student> GetDbSet()
        {
            return Context.students;
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public List<student> GetList()
        {
            return GetDbSet().ToList();
        }
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="sno">学生编号</param>
        /// <returns></returns>
        public student GetModel(string sno)
        {
            return GetDbSet().Find(sno);
            //IQueryable<student> query = GetDbSet().Where(q=>q.sno==sno);
            //student stu= query.FirstOrDefault();
            //return stu;
        }
        /// <summary>
        /// 获取对象及关联数据
        /// </summary>
        /// <param name="sno">学生编号</param>
        /// <returns></returns>
        public student GetModel_Related(string sno)
        {
            student model = GetModel(sno);
            if (model != null)
                model.scores = Context.scores.Where(q => q.sno == sno).ToList();
            return model;
        }
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="model">学生对象</param>
        /// <returns></returns>
        public bool Add(student model)
        {
            GetDbSet().Add(model);
            return Context.SaveChanges() > 0;
        }
    }
}
